package cn.saltybe.analysis.util;

import com.google.gson.Gson;

import java.io.Serializable;

public class ApiResponse<T> implements Serializable {
    private boolean status;
    private int code;
    private String message;
    private Object data;

    public ApiResponse(ApiResponseStatus status, ApiResponseCode code, String message, T data) {
        this.setStatus(status);
        this.setCode(code);
        this.setMessage(message);
        this.setData(data);
    }

    public ApiResponse(ApiResponseStatus status, ApiResponseCode code, String message) {
        this.setStatus(status);
        this.setCode(code);
        this.setMessage(message);
    }

    public static <T> ApiResponse<T> buildSuccessResponse(T data) {
        return new ApiResponse<T>(ApiResponseStatus.OK, ApiResponseCode.NORMAL, null, data);
    }

    public static <T> ApiResponse<T> buildSuccessResponse() {
        return new ApiResponse<T>(ApiResponseStatus.OK, ApiResponseCode.NORMAL, null);
    }

    public static <T> ApiResponse<T> buildFailResponse(ApiResponseStatus responseStatus, ApiResponseCode responseCode, String message) {
        return new ApiResponse<T>(responseStatus, responseCode, message);
    }


    public boolean getStatus() {
        return status;
    }

    public void setStatus(ApiResponseStatus status) {
        this.status = status.getStatus();
    }

    public int getCode() {
        return code;
    }

    public void setCode(ApiResponseCode code) {
        this.code = code.getCode();
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return new Gson().toJsonTree(this).getAsJsonObject().toString();
    }
}
